Extension { #name : 'BlockClosure' }

{ #category : '*System-Benchmark' }
BlockClosure >> bench [
	"Answer a BenchmarkResult with the number of times the receiver was executed in 5 seconds"

	"[3.14 printString] bench"

	^ self benchFor: 5 seconds
]

{ #category : '*System-Benchmark' }
BlockClosure >> benchCompareTo: aBlock [
	"Answer a BenchmarkComparison of the receiver to the argument."

	^ self bench compareTo: aBlock bench
]

{ #category : '*System-Benchmark' }
BlockClosure >> benchFor: duration [

	"Run me for duration and return a BenchmarkResult"

	"[ 100 factorial ] benchFor: 2 seconds"

	| count run exception started |
	count := 0.
	run := true.
	exception := nil. "Used to catch possible exception in the wakeup thread"
	[ [ duration wait. ]
			on: Exception
			do: [ :e | exception := e ].
		run := false ] forkAt: Processor timingPriority - 1.
	started := Time millisecondClockValue.
	[ run ] whileTrue: [
		self value.
		count := count + 1 ].
	exception ifNotNil: [ exception signal ].
	^ BenchmarkResult new
		  iterations: count;
		  elapsedTime: (Time millisecondsSince: started) milliSeconds;
		  yourself
]
